package com.wocai.jrt.orgadmin.news.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wocai.jrt.orgadmin.employee.model.Employee;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeService;
import com.wocai.jrt.orgadmin.message.model.MessageLog;
import com.wocai.jrt.orgadmin.message.service.spi.MessageLogService;
import com.wocai.jrt.orgadmin.news.dao.NewsDao;
import com.wocai.jrt.orgadmin.news.model.News;
import com.wocai.jrt.orgadmin.news.model.NewsBean;
import com.wocai.jrt.orgadmin.news.service.spi.NewsService;
import com.wocai.jrt.orgadmin.utils.MessagePush;
import com.wocai.jrt.orgadmin.utils.UserUtils;
import com.vteba.service.generic.impl.MyBatisServiceImpl;
import com.vteba.tx.jdbc.mybatis.spi.BaseDao;
import com.vteba.utils.id.ObjectId;

/**
 * 新闻相关的service业务实现。
 * @author yinlei
 * @date 2015-8-10 15:45:28
 */
@Named
public class NewsServiceImpl extends MyBatisServiceImpl<News, NewsBean, String> implements NewsService {
	// 添加了方法后，记得改为private
	protected NewsDao newsDao;
	private static final Logger LOGGER = LoggerFactory.getLogger(NewsServiceImpl.class);
	@Inject 
	private EmployeeService employeeServiceImpl;
	@Inject
	private MessageLogService messageLogServiceImpl;
	@Override
	@Inject
	public void setBaseDao(BaseDao<News, NewsBean, String> newsDao) {
		this.baseDao = newsDao;
		this.newsDao = (NewsDao) newsDao;
	}
	
	/**
	 * 新闻发布时推送消息
	 * @author zhangyz
	 * @data 2015年9月9日19:40:34
	 */
	@Override
	public void createNewsMessage(News model) {
		try {
			Employee emp = new Employee();
			emp.setOrgId(UserUtils.getOrgId());
			List<Employee> empList = employeeServiceImpl.pagedList(emp);
			List<MessageLog> msgList = new ArrayList<MessageLog>();
			Date date = new Date();
			String newsId = model.getId();
			String orgId = model.getOrgId();
			for (Employee employee : empList) {
				// 保存消息日志
				MessageLog messageLog = new MessageLog();
				messageLog.setId(ObjectId.get().toString());
				messageLog.setEmployeeId(employee.getEmployeeId());
				messageLog.setSender("机构消息");
				messageLog.setSource("机构后台-新闻消息");
				messageLog.setSkipType(5);
				messageLog.setSkipKey(newsId);
				messageLog.setMessageType(1);
				messageLog.setType(2);
				messageLog.setState(0);
				messageLog.setOrgId(orgId);
				messageLog.setCreateTime(date);
				messageLog.setMessage(model.getTitle());
				msgList.add(messageLog);
			}	
				int i = messageLogServiceImpl.msgLogInsertBatch(msgList);
				int size = msgList.size();
				if (i == size) {
					LOGGER.error("save all message for news success ");
				} else if(i < size){
					LOGGER.error("save part message for news success ");
				}else if(i == 0) {
					LOGGER.error("save all message for news error ");
				}
			// 极光推送:向机构所有员工推送消息
			MessagePush messagePush = new MessagePush(model.getTitle(), "最新新闻");
			messagePush.sendPushTag(UserUtils.getOrgId());
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("save message for news error ");
		}
		
	}
}
